package com.yihuchess.simplechess;

import android.app.Activity;
import android.os.Bundle;

import android.util.Log;
import com.yihuchess.bluetoothConnect.BtKeyListener;
import com.yihuchess.simplechess.BoardView;
import com.yihuchess.bluetoothConnect.BtInterfaceMananger;

public class GameActivity extends Activity {
    private static final String TAG = "GameActivity";
    BoardView mBoardView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        mBoardView = (BoardView)findViewById(R.id.boardView);

        BtInterfaceMananger.mBtKeyCaller = mBtKeyListener;
    }

	@Override
	protected void onDestroy() {
        BtInterfaceMananger.mBtKeyCaller = null;
		super.onDestroy();
	}

    public BtKeyListener mBtKeyListener = new BtKeyListener() {

        public void onBoardKey(int key, byte[] msg) {
            int boardX, boardY;

            if (BtInterfaceMananger.mBtBoardInterface == null)
                return;

            switch (key) {
                case 1:  // info
                    break;
                case 2:  // all -- single lights.
                    break;
                case 3: // prev qipu
                    break;
                case 4: // next qipu
                    //onClick(mPassBtn);
                    break;
                case 5:
                    //onClick(mBackBtn);
                    break;
                case 6:
                    //onClick(mRedoBtn);
                    break;
                case 7:
                    //onClick(mUndoBtn);
                    break;
                case 8:
                    if (msg != null) {
                        //moveStone
                        boardX = msg[3] - 1;
                        boardY = msg[4] - 1;

                        if ((boardX >= 2) && (boardX <= 17) && ((boardX % 2) == 0)) {
                            if ((boardY >= 2) && (boardY <= 19) && (boardY % 2 == 0)) {
                                boardX = boardX / 2 - 1;
                                boardY = boardY / 2 - 1;
                                boardY = 7 - boardY;

                                Log.e(TAG, "after boardX:" + boardX + " boardY" + boardY);
                                mBoardView.onePieceMove(boardX, boardY);
                            }
                        }
                    } else {
                        //onClick(mConfirmBtn);
                    }
                    break;
                default:
                    break;
            }
        }
    };

}

